﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.IO;
using Portal.Systems.Model;
using Portal.Database.Service;
using Portal.Web.Utility;
using Portal.Core;
using Portal.Utility;
using Portal.Systems.Bussiness.Template;

namespace Portal.Systems.Bussiness
{
    public class RoleWorkingWorkflowProcessBll 
    {
        public static RoleWorkingWorkflowCls[] ReadingRoleWorkingWorkflow(string SiteId, string RoleId)
        {
            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
            SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
            string AssetCode = WebSessionUtility.GetCurrentLoginUser().AssetCode;
            string DataLanguage = WebConfig.GetDataLanguage(OSiteParam);
            string Query =
                " Insert into TableRoleWorkingWorkflow(RoleWorkingWorkflowId, RoleId, frkWorkflowControlId, AssetCode) "+
                " ("+
                "       select newid(),@RoleId, WorkflowControlId, @AssetCode from TableWorkflowControl where TableWorkflowControl.AssetCode=@AssetCode " +
                "           and WorkflowControlId not in "+
                "           ("+
                "               select frkWorkflowControlId from TableRoleWorkingWorkflow"+
                "                   where TableRoleWorkingWorkflow.AssetCode=@AssetCode and RoleId=@RoleId" +
                "           )"+
                " );"+
                " select TableRoleWorkingWorkflow.*,frkDataLanguageId,Subject=TableWorkflowControl.Title from TableRoleWorkingWorkflow " +
                "       join TableWorkflowControl on TableWorkflowControl.WorkflowControlId=frkWorkflowControlId"+
                "   where TableRoleWorkingWorkflow.AssetCode=@AssetCode and RoleId=@RoleId";
            DataSet dsResult =
                    DBService.GetDataSet(null, Query,
                    new DbParam[] 
                    { 
                        new DbParam("AssetCode", AssetCode),
                        new DbParam("RoleId", RoleId),
                    });
            if (dsResult.Tables[0].Rows.Count == 0)
            {
                return new RoleWorkingWorkflowCls[0];
            }
            RoleWorkingWorkflowCls[] RoleWorkingWorkflows = RoleWorkingWorkflowParser.ParseFromDataTable(dsResult.Tables[0]);
            dsResult.Clear();
            dsResult.Dispose();
            return RoleWorkingWorkflows;
        }


        public static RoleWorkingWorkflowCls[] ReadingAllowAccessRoleWorkingWorkflow(string SiteId, string RoleId)
        {
            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
            SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
            string AssetCode = WebSessionUtility.GetCurrentLoginUser().AssetCode;
            string DataLanguage = WebConfig.GetDataLanguage(OSiteParam);
            string Query =
                " select TableRoleWorkingWorkflow.*,frkDataLanguageId,Subject=TableWorkflowControl.Title from TableRoleWorkingWorkflow " +
                "       join TableWorkflowControl on TableWorkflowControl.WorkflowControlId=frkWorkflowControlId" +
                "   where [AllowAccess]=1 and TableRoleWorkingWorkflow.AssetCode=@AssetCode and RoleId=@RoleId order by SortIndex";
            DataSet dsResult =
                    DBService.GetDataSet(null, Query,
                    new DbParam[] 
                    { 
                        new DbParam("AssetCode", AssetCode),
                        new DbParam("RoleId", RoleId),
                    });
            if (dsResult.Tables[0].Rows.Count == 0)
            {
                return new RoleWorkingWorkflowCls[0];
            }
            RoleWorkingWorkflowCls[] RoleWorkingWorkflows = RoleWorkingWorkflowParser.ParseFromDataTable(dsResult.Tables[0]);
            dsResult.Clear();
            dsResult.Dispose();
            return RoleWorkingWorkflows;
        }

        public static RoleWorkingWorkflowCls[] ReadingAllowAccessRoleWorkingWorkflow(string SiteId, string RoleId, string WorkflowControlType)
        {
            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
            SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
            string AssetCode = WebSessionUtility.GetCurrentLoginUser().AssetCode;
            string DataLanguage = WebConfig.GetDataLanguage(OSiteParam);
            string Query =
                " select TableRoleWorkingWorkflow.*,frkDataLanguageId,Subject=TableWorkflowControl.Title from TableRoleWorkingWorkflow " +
                "       join TableWorkflowControl on TableWorkflowControl.WorkflowControlId=frkWorkflowControlId" +
                "   where [AllowAccess]=1 and TableRoleWorkingWorkflow.AssetCode=@AssetCode and RoleId=@RoleId and WorkflowControlType=@WorkflowControlType "+
                "       order by SortIndex";
            DataSet dsResult =
                    DBService.GetDataSet(null, Query,
                    new DbParam[] 
                    { 
                        new DbParam("AssetCode", AssetCode),
                        new DbParam("RoleId", RoleId),
                        new DbParam("WorkflowControlType", WorkflowControlType),
                    });
            if (dsResult.Tables[0].Rows.Count == 0)
            {
                return new RoleWorkingWorkflowCls[0];
            }
            RoleWorkingWorkflowCls[] RoleWorkingWorkflows = RoleWorkingWorkflowParser.ParseFromDataTable(dsResult.Tables[0]);
            dsResult.Clear();
            dsResult.Dispose();
            return RoleWorkingWorkflows;
        }


        public static bool CheckAllowAccessWorkflow(string SiteId, string RoleId,string WorkflowDefId)
        {
            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
            SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
            string AssetCode = WebSessionUtility.GetCurrentLoginUser().AssetCode;
            string DataLanguage = WebConfig.GetDataLanguage(OSiteParam);
            string Query =
                " select RoleWorkingWorkflowId from TableRoleWorkingWorkflow " +
                "       join TableWorkflowControl on TableWorkflowControl.WorkflowControlId=frkWorkflowControlId" +
                "   where WorkflowDefId=@WorkflowDefId and [AllowAccess]=1 " +
                " and TableRoleWorkingWorkflow.AssetCode=@AssetCode and RoleId=@RoleId";
            DataTable dtResult =
                    DBService.GetDataTable(null, Query,
                    new DbParam[] 
                    { 
                        new DbParam("AssetCode", AssetCode),
                        new DbParam("RoleId", RoleId),
                        new DbParam("WorkflowDefId", WorkflowDefId),
                    });
            return dtResult.Rows.Count > 0;
        }

        public static RoleWorkingWorkflowCls CreateRoleWorkingWorkflowModel(
            string SiteId,
            string RoleWorkingWorkflowId)
        {
            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
            SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
            string AssetCode = WebSessionUtility.GetCurrentLoginUser().AssetCode;
            string DataLanguage = WebConfig.GetDataLanguage(OSiteParam);
            string Query =
                " select TableRoleWorkingWorkflow.*,Subject=TableWorkflowControl.Title from TableRoleWorkingWorkflow " +
                "       join TableWorkflowControl on TableWorkflowControl.WorkflowControlId=frkWorkflowControlId"+
                "   where RoleWorkingWorkflowId=@RoleWorkingWorkflowId";
            DataSet dsResult =
                    DBService.GetDataSet(null, Query,
                    new DbParam[] 
                    { 
                        new DbParam("RoleWorkingWorkflowId", RoleWorkingWorkflowId),
                    });
            if (dsResult.Tables[0].Rows.Count == 0)
            {
                return null;
            }
            RoleWorkingWorkflowCls ORoleWorkingWorkflow = RoleWorkingWorkflowParser.ParseFromDataRow(dsResult.Tables[0].Rows[0]);
            dsResult.Clear();
            dsResult.Dispose();
            return ORoleWorkingWorkflow;
        }


        public static void SaveRoleWorkingWorkflow(
            string SiteId,
            string RoleId,
            RoleWorkingWorkflowCls[] RoleWorkingWorkflow)
        {
            SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
            IDbTransaction Trans = DBService.BeginTransaction();
            try
            {
                for (int iIndex = 0; iIndex < RoleWorkingWorkflow.Length; iIndex++)
                {
                    DBService.Update(Trans, "TableRoleWorkingWorkflow", "RoleWorkingWorkflowId", RoleWorkingWorkflow[iIndex].RoleWorkingWorkflowId,
                            new DbParam[] 
                    { 
                        new DbParam("AllowAccess",RoleWorkingWorkflow[iIndex].AllowAccess),
                    });
                }
                Trans.Commit();
            }
            catch (Exception ex)
            {
                Trans.Rollback();
                throw (ex);
            }
        }
    }
}
